package com.cmccsi.service.impl;



import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;





import org.apache.log4j.Logger;

import com.cmccsi.entity.qxgroupMemberDto;
import com.cmccsi.mapper.qxdeptMemberDtoMapper;
import com.cmccsi.mapper.qxgroupMemberDtoMapper;
import com.cmccsi.service.GroupMemberService;
import com.cmccsi.utils.MybatisUtil;

public class GroupMemberServiceImpl implements GroupMemberService {
	
	private SqlSessionFactory sessionFactory = MybatisUtil.getInstance();

	private Logger loger = Logger.getLogger(DomainServiceImpl.class);
	@Override
	public Boolean delete(Long id) {
		SqlSession session = sessionFactory.openSession();
		try {
			qxgroupMemberDtoMapper groupMMapper = session.getMapper(qxgroupMemberDtoMapper.class);
			groupMMapper.deleteByPrimaryKey(id);
			session.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			loger.error(e.getMessage(), e);
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public Boolean update(qxgroupMemberDto grpM) {
		// TODO Auto-generated method stub
		SqlSession session = sessionFactory.openSession();
		try {

			qxgroupMemberDtoMapper groupMMapper = session.getMapper(qxgroupMemberDtoMapper.class);

			List<qxgroupMemberDto> lstDatas = groupMMapper
					.selectByUseridAndGroupId(grpM.getGroupId(),
							grpM.getUserId());
			if (lstDatas != null && lstDatas.size() > 0) {
				grpM.setId(lstDatas.get(0).getId());
				groupMMapper.updateByPrimaryKey(grpM);
				session.commit();
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			loger.error(e.getMessage(), e);
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public qxgroupMemberDto find(Long grpId) {
		// TODO Auto-generated method stub
		qxgroupMemberDto grpm = null;
		SqlSession session = sessionFactory.openSession();
		try {
			qxgroupMemberDtoMapper groupMMapper = session.getMapper(qxgroupMemberDtoMapper.class);
			grpm =groupMMapper.selectByPrimaryKey(grpId);
		} catch (Exception e) {
			e.printStackTrace();
			loger.error(e.getMessage(), e);
		} finally {
			session.close();
		}
		return grpm;
	}


	@Override
	public Boolean save(qxgroupMemberDto grpM) {
		// TODO Auto-generated method stub
		SqlSession session = sessionFactory.openSession();
		try {
			qxgroupMemberDtoMapper groupMMapper = session.getMapper(qxgroupMemberDtoMapper.class);
			List<Long> lstMemberIds=groupMMapper.selectMemberIdsByGroupId(grpM.getGroupId());
			if(lstMemberIds!=null&&lstMemberIds.contains(grpM.getUserId()))
			{
				return true;
			}
			groupMMapper.insert(grpM);
			session.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();			
			loger.error(e.getMessage(), e);	
			loger.error("出错的Group的GroupID："+grpM.getGroupId());	
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public Boolean deleteByGroupidAndMemberids(Long grpid, List<Long> usersids) {
		// TODO Auto-generated method stub
		SqlSession session = sessionFactory.openSession();
		try {
			qxgroupMemberDtoMapper groupMMapper = session.getMapper(qxgroupMemberDtoMapper.class);
			groupMMapper.deleteByGroupidAndMemberids(grpid,(Long[]) usersids.toArray());
			session.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			loger.error(e.getMessage(), e);
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public Boolean deleteByGroupid(Long grpId) {
		// TODO Auto-generated method stub
		SqlSession session = sessionFactory.openSession();
		try {
			qxgroupMemberDtoMapper groupMMapper = session.getMapper(qxgroupMemberDtoMapper.class);
		    List<Long> lstMemberids=	groupMMapper.selectMemberIdsByGroupId(grpId);		    
		    deleteByGroupidAndMemberids(grpId,lstMemberids);
			session.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			loger.error(e.getMessage(), e);
		} finally {
			session.close();
		}
		return false;
	}
	
	@Override
	public Boolean deleteByMemberids(List<Long> usersids) {
		// TODO Auto-generated method stub
		SqlSession session = sessionFactory.openSession();
		try {
			qxgroupMemberDtoMapper groupMMapper = session.getMapper(qxgroupMemberDtoMapper.class);			
			Long[] longArr = new Long[usersids.size()];
			usersids.toArray(longArr);			
			groupMMapper.deleteByMemberids(longArr);
			session.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			loger.error(e.getMessage(), e);
		} finally {
			session.close();
		}
		return false;
	}

}
